import { sequelize, Sequelize } from '../config/database.js';
import User from './User.js';
import Course from './Course.js';
import Enrollment from './Enrollment.js';

// 初始化所有模型
const db = {};

db.User = User.initModel(sequelize);
db.Course = Course.initModel(sequelize);
db.Enrollment = Enrollment.initModel(sequelize);

// 建立模型关联
Object.keys(db).forEach((modelName) => {
  if (db[modelName].associate) {
    db[modelName].associate(db);
  }
});

// 添加sequelize和Sequelize到db对象
db.sequelize = sequelize;
db.Sequelize = sequelize.Sequelize;

// 导出模型和Sequelize实例
export default db;

// 导出模型类
export { User, Course, Enrollment };
